package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:00
 * @Version V1.0
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;


    @Override
    public Member findMemberByTelephone(String telephone) {
        return memberDao.findMemberByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        // 注册会员（密码进行加密）
        if(member.getPassword()!=null && !member.getPassword().equals("")){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    // 使用注册时间查询，获取当前时间之前的会员数量统计
    // List<String> monthsList-->[2018-09, 2018-10, 2018-11, 2018-12, 2019-01, 2019-02, 2019-03, 2019-04, 2019-05, 2019-06, 2019-07, 2019-08]
    @Override
    public List<Integer> findMemberCountByRegTime(List<String> monthsList) {
        List<Integer> memeberCountList = new ArrayList<Integer>();
        if(monthsList!=null && monthsList.size()>0){
            for (String months : monthsList) {
                String regTime = months+"-31";
                Integer memeberCount = memberDao.findMemberCountByRegTime(regTime);
                memeberCountList.add(memeberCount);
            }
        }
        return memeberCountList;
    }
}
